package com.example.project.Service.Impl;

import com.example.project.Service.ResourceService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ResourceServiceImpl implements ResourceService {
    @Override
    public String upLoadResource(MultipartFile files){
        String path = System.getProperty("user.dir");
        File resourcePath = new File(path+"/src//main/resources/static/resources");
        if(!resourcePath.exists()){
            resourcePath.mkdirs();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String resourceName = sdf.format(new Date())+"_"+files.getOriginalFilename();
        String t = resourceName.substring(resourceName.lastIndexOf(".") + 1, resourceName.length()).toLowerCase();
        if(!t.equals("zip")&&!t.equals("rar")){
            return "-2";
        }
        File resourceResultPath = new File(resourcePath+"/"+resourceName);
        try {
            files.transferTo(resourceResultPath);
            String s = resourceResultPath.getCanonicalPath();
            return s;
        } catch (IOException e) {
            return "-1";
        }
    }
}
